# -- coding: utf-8 --
import os
import sys
import tensorflow as tf
import argparse
from tensorflow import gfile    #用于多平台文件路径格式兼容
from six.moves import xrange  
import cv2
import numpy as np
from openslide import open_slide, OpenSlideUnsupportedFormatError
import re
import pdb
import random
from PIL import Image, ImageDraw

FLAGS = tf.app.flags.FLAGS
user = 'ramsley'
GLOB_WSI_TYPE_PATTEN = '*.ndpi'
GLOB_XML_TYPE_PATTEN = '*.xml'
GLOB_MASK_TYPE_PATTEN = '*.tif'

#训练集、测试集总Dir(如何通过外部设定？)
TRAIN_DATASET = '/home/' + user + '/DataSet/prostate/1-3/trainingData/'
VALIDATION_DATASET = '/home/' + user + '/DataSet/prostate/1-3/testingData/'

#训练集、测试集：原图、mask
TRAIN_TUMOR_WSI_DIR = TRAIN_DATASET + 'Train_Tumor/'
TRAIN_NORMAL_WSI_DIR = TRAIN_DATASET + 'Train_Normal/'
TRAIN_TUMOR_MASK_DIR = TRAIN_DATASET + 'groundTruth/mask/'
TRAIN_TUMOR_XML_DIR = TRAIN_DATASET + 'groundTruth/xml/'

VALIDATION_WSI_DIR = VALIDATION_DATASET + 'TestSet/'
VALIDATION_TUMOR_MASK_DIR = VALIDATION_DATASET + 'groundTruth/mask/'
VALIDATION_TUMOR_XML_DIR = VALIDATION_DATASET + 'groundTruth/xml/'

#训练集：分类patches、txt、tfrecords
PROSTATE_PATCHES_TRAIN = TRAIN_DATASET + 'patch-based-classification/'							
PROSTATE_PATCHES_TRAIN_TUMOR_NEG = PROSTATE_PATCHES_TRAIN + 'tumor-label-0/'					#Tumor WSI非标注区（label = 0）
PROSTATE_PATCHES_TRAIN_NORMAL_NEG = PROSTATE_PATCHES_TRAIN + 'label-0/'
PROSTATE_PATCHES_TRAIN_TUMOR_POS = PROSTATE_PATCHES_TRAIN + 'label-1/'
PROSTATE_PATCHES_TRAIN_TXT = PROSTATE_PATCHES_TRAIN + 'label-txt/'
TRAIN_TFRECORDS_DIR = PROSTATE_PATCHES_TRAIN + 'tf-records/'
TRAIN_TISSUE_CONTOUR_DIR = PROSTATE_PATCHES_TRAIN + 'OverviewRef/ov_image/'						#带轮廓的缩略图
TRAIN_PATCH_DISTRIBUTION_DIR = PROSTATE_PATCHES_TRAIN + 'OverviewRef/patch_distribution_image/' #取样标记图

#测试集：分类patches、txt、tfrecords
PROSTATE_PATCHES_VALIDATION = VALIDATION_DATASET + 'patch-based-classification/'
PROSTATE_PATCHES_VALIDATION_NEG = PROSTATE_PATCHES_VALIDATION + 'label-0/'
PROSTATE_PATCHES_VALIDATION_POS = PROSTATE_PATCHES_VALIDATION + 'label-1/'
PROSTATE_PATCHES_VALIDATION_TXT = PROSTATE_PATCHES_VALIDATION + 'label-txt/'
VALIDATION_TFRECORDS_DIR = PROSTATE_PATCHES_VALIDATION + 'tf-records/'
VALIDATION_TISSUE_CONTOUR_DIR = PROSTATE_PATCHES_VALIDATION + 'OverviewRef/ov_image/'
VALIDATION_PATCH_DISTRIBUTION_DIR = PROSTATE_PATCHES_VALIDATION + 'OverviewRef/patch_distribution_image/'

#checkpoint存储路径
PRETRAIN_CKPT_DIR = '/home/ramsley/workspace/pretrained_ckpt/'
INCEPTION_V3_CKPT = PRETRAIN_CKPT_DIR + 'inception_v3'
INCEPTION_V4_CKPT = PRETRAIN_CKPT_DIR + 'inception_v4'
INCEPTION_RESNET_V2_CKPT = PRETRAIN_CKPT_DIR + 'inception_resnet_v2'#/inception_resnet_v2.ckpt'
RESNET_V2_50_CKPT = PRETRAIN_CKPT_DIR + 'resnet_v2_50'
RESNET_V2_101_CKPT = PRETRAIN_CKPT_DIR + 'resnet_v2_101'
RESNET_V2_152_CKPT = PRETRAIN_CKPT_DIR + 'resnet_v2_152'
VGG_16_CKPT = PRETRAIN_CKPT_DIR + 'vgg_16'
VGG_19_CKPT = PRETRAIN_CKPT_DIR + 'vgg_19'

#样本命名前缀
PATCH_NORMAL_PREFIX = 'normal_'
PATCH_TUMOR_PREFIX = 'tumor_'

#以下全局变量可通过命令行输入
tf.app.flags.DEFINE_string('xmlDir', TRAIN_TUMOR_XML_DIR, '''''')
tf.app.flags.DEFINE_string('slideDir', TRAIN_TUMOR_WSI_DIR, '''''')
tf.app.flags.DEFINE_string('maskDir', TRAIN_TUMOR_MASK_DIR, '''''')
tf.app.flags.DEFINE_string('output_NEG_patchDir', PROSTATE_PATCHES_TRAIN_NORMAL_NEG, '''''')
tf.app.flags.DEFINE_string('output_POS_patchDir', PROSTATE_PATCHES_TRAIN_TUMOR_NEG, '''''')
tf.app.flags.DEFINE_string('output_txtDir', PROSTATE_PATCHES_TRAIN_TXT, '''''')